//
//  TitlesViewController.m
//  first app
//
//  Created by 林 小明 on 14-7-5.
//
//

#import "TitlesViewController.h"
#import "TitleCell.h"
#import "InfoViewController.h"
#import "FileCacheManager.h"
#import "ContentParseCenter.h"

@interface TitlesViewController ()

@end

@implementation TitlesViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showPlaystatus:) name:@"Notification_RefreshCache" object:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [contentLinkIndex count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TitleCell *cell = (TitleCell*)[tableView
      dequeueReusableCellWithIdentifier:@"TitleCell"];
    
    NSString *title = [(NSDictionary*)[contentLinkIndex objectAtIndex:indexPath.row] objectForKey:@"chapterTitle"];
    
    cell.nameLabel.text = title;
    
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // Delete the row from the data source
		[contentLinkIndex removeObjectAtIndex:indexPath.row];
		[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UIViewController *destination = [segue destinationViewController];
    InfoViewController *infoViewController = (InfoViewController *)destination;
    
    NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
    
    [infoViewController setViewInfo:[contentLinkIndex objectAtIndex:indexPath.row]];
}

- (void) setContentLinkIndex:(NSMutableArray*)content
{
    contentLinkIndex = content;
}

- (void) setContentParam:(NSDictionary*)param
{
    contentParam = param;
}

- (void)showPlaystatus:(NSNotification*) aNotification
{
    NSMutableDictionary *playStatusInfo = [aNotification object];
    
    //NSString *url = [contentParam objectForKey:@"programURL"];
    
    NSString *filename = [contentParam objectForKey:@"programCacheName"];
    
    if( [filename isEqualToString: [playStatusInfo objectForKey:@"filename"]] )
    {
        NSMutableArray* titleIndexs = [[FileCacheManager sharedSingleton] getContentFromCacheFile:filename];
        
        [self setContentLinkIndex:titleIndexs];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [self.tableView reloadData];
        NSLog(@"reload");
    }
}

@end
